﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace DigitalWhiteBoardGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region Variables
        // Pen Properties
        Color penColor;
        int penSize;
        int smallPenSize = 2;
        int mediumPenSize = 8;
        int largePenSize = 16;

        // Cursor Properties
        bool penDrag = false;
        int NewX;
        int NewY;
        int oldX;
        int oldY;

        // Canvas Properties
        Bitmap foregroundBMP;
        Bitmap backgroundBMP;
        Bitmap transBMP;
        bool selectMode = true;

        //4.3
        //Container Properties
        bool menuDrag = false;

        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            this.TopMost = true;

#if DEBUG
            this.TransparencyKey = pictureBox1.BackColor;
#endif

            // Set defaults for pen size/color/mode/transparancy
            smallButton_Click(sender, e);
            blackButton_Click(sender, e);
            modeButton_Click(sender, e);
            transBMP = new Bitmap(pictureBox1.Width, pictureBox1.Height);

            setBG();
        }

        private void Form1_Maximize(object sender, EventArgs e)
        {

            if (this.WindowState.Equals(FormWindowState.Maximized))
            {
                foregroundBMP = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                pictureBox1.Image = foregroundBMP;
                setBG();
            }
#if (DEBUG)
            this.WindowState = FormWindowState.Normal;
            pictureBox1.BorderStyle = BorderStyle.Fixed3D;
#endif
        }


        private void Form1_Refresh(object sender, EventArgs e)
        {
            setBG();
        }

        private void setBG()
        {
            this.Hide();

            if (!selectMode)
            {
                pictureBox1.BackgroundImage = transBMP;
            }
            else
            {
                    backgroundBMP = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    Graphics bgGraphics = Graphics.FromImage(backgroundBMP);
                    bgGraphics.CopyFromScreen(this.PointToScreen(pictureBox1.Location).X, this.PointToScreen(pictureBox1.Location).Y, 0, 0, pictureBox1.Size, CopyPixelOperation.SourceCopy);
                    pictureBox1.BackgroundImage = backgroundBMP;
                    bgGraphics.Dispose();
            }

            this.Show();
        }

        //Brute SaveAs: 4.2
    #region SaveCode
        /*//Save: 2.0
        private void saveButton_Click(object sender, EventArgs e)
        {
            Bitmap outputBMP = new Bitmap(backgroundBMP);
            Graphics outputGraphics = Graphics.FromImage(outputBMP);

            outputGraphics.DrawImage(foregroundBMP, new Point(0, 0));

            outputBMP.Save("C:\\output1.bmp");
            outputBMP.Dispose();
            outputGraphics.Dispose();
        }*/

        //Brute SaveAs: 4.2
        private void saveAsButton_Click(object sender, EventArgs e)
        {
            String fileName = richTextBox1.Text;

            Bitmap outputBMP = new Bitmap(backgroundBMP);
            Graphics outputGraphics = Graphics.FromImage(outputBMP);

            outputGraphics.DrawImage(foregroundBMP, new Point(0, 0));

            String destination = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\" + fileName + ".bmp";
            
            outputBMP.Save(destination);
            outputBMP.Dispose();
            outputGraphics.Dispose();

            richTextBox1.Text = "FileName";
        }
    #endregion

        //Menu: 4.3
    #region MenuCode
        private void myContainer_MouseDown(object sender, MouseEventArgs e)
        {
            menuDrag = true;
        }

        private void myContainer_MouseMove(object sender, MouseEventArgs e)
        {
            if (menuDrag)
            {   myContainer.SetBounds(e.X + myContainer.Bounds.Left, e.Y + myContainer.Bounds.Top, myContainer.Width, myContainer.Height);  }
        }

        private void myContainer_MouseUp(object sender, MouseEventArgs e)
        {
            menuDrag = false;
        }
    #endregion

        private void clearButton_Click(object sender, EventArgs e)
        {
            foregroundBMP = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Image = foregroundBMP;
        }

        #region DrawCode
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (selectMode)
            {
                Graphics g = Graphics.FromImage(foregroundBMP);

                SolidBrush myBrush = new SolidBrush(penColor);

                g.FillEllipse(myBrush, new Rectangle(e.X - (penSize / 2), e.Y - (penSize / 2), penSize, penSize));

                g.Dispose();

                pictureBox1.Image = foregroundBMP;
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            penDrag = true;
            NewX = e.X;
            NewY = e.Y;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (selectMode) //Marc: fixs glitch where if you click on a drawn line while in select mode you could still draw
            {
                Graphics g = Graphics.FromImage(foregroundBMP);

                Pen myPen = new Pen(penColor, penSize);
                SolidBrush myBrush = new SolidBrush(penColor);

                if (penDrag == true)
                {
                    g.DrawLine(myPen, oldX, oldY, e.X, e.Y);
                    oldX = e.X;
                    oldY = e.Y;
                    g.FillEllipse(myBrush, new Rectangle(e.X - (penSize / 2), e.Y - (penSize / 2), penSize, penSize));
                }
                oldX = e.X;
                oldY = e.Y;

                g.Dispose();

                pictureBox1.Image = foregroundBMP;
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            penDrag = false;
        }
        #endregion

        #region [size]Button_Click()
        private void smallButton_Click(object sender, EventArgs e)
        {
            penSize = smallPenSize;
            resetSizeButtons();
            smallButton.BackColor = Control.DefaultForeColor;
            smallButton.ForeColor = Control.DefaultBackColor;
        }

        private void mediumButton_Click(object sender, EventArgs e)
        {
            penSize = mediumPenSize;
            resetSizeButtons();
            mediumButton.BackColor = Control.DefaultForeColor;
            mediumButton.ForeColor = Control.DefaultBackColor;
        }

        private void largeButton_Click(object sender, EventArgs e)
        {
            penSize = largePenSize;
            resetSizeButtons();
            largeButton.BackColor = Control.DefaultForeColor;
            largeButton.ForeColor = Control.DefaultBackColor;
        }

        private void resetSizeButtons()
        {
            smallButton.BackColor = Control.DefaultBackColor;
            smallButton.ForeColor = Control.DefaultForeColor;
            mediumButton.BackColor = Control.DefaultBackColor;
            mediumButton.ForeColor = Control.DefaultForeColor;
            largeButton.BackColor = Control.DefaultBackColor;
            largeButton.ForeColor = Control.DefaultForeColor;
        }
        #endregion

        #region [Color]Button_Click()
        private void blackButton_Click(object sender, EventArgs e)
        {
            penColor = Color.Black;
            resetColorButtons();
            blackButton.BackColor = Color.Black;
        }

        private void redButton_Click(object sender, EventArgs e)
        {
            penColor = Color.Red;
            resetColorButtons();
            redButton.BackColor = Color.Red;
        }

        private void greenButton_Click(object sender, EventArgs e)
        {
            penColor = Color.Green;
            resetColorButtons();
            greenButton.BackColor = Color.Green;
        }

        private void blueButton_Click(object sender, EventArgs e)
        {
            penColor = Color.Blue;
            resetColorButtons();
            blueButton.BackColor = Color.Blue;
        }

        private void yellowButton_Click(object sender, EventArgs e)
        {
            penColor = Color.Yellow;
            resetColorButtons();
            yellowButton.BackColor = Color.Yellow;
        }

        private void resetColorButtons()
        {
            blackButton.BackColor = Control.DefaultBackColor;
            redButton.BackColor = Control.DefaultBackColor;
            greenButton.BackColor = Control.DefaultBackColor;
            blueButton.BackColor = Control.DefaultBackColor;
            yellowButton.BackColor = Control.DefaultBackColor;
        }
        #endregion


        private void modeButton_Click(object sender, EventArgs e)
        {
            if (!selectMode)
            {
                selectMode = true;
                modeButton.Text = "Click to Select";
            }
            else
            {
                selectMode = false;
                modeButton.Text = "Click to Draw";
            }
            setBG();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}